/**
 * Created by henian.xu on 2018/10/15.
 *
 */

export default {
    data() {
        return {
            cacheArea: {},
            cacheAreaMap: {},
            cancelSource: null,
        };
    },
    props: {
        value: {
            type: Number,
            default: 0,
        },
        pId: {
            type: Number,
            default: undefined,
        },
    },
    methods: {
        getChildren(data) {
            return new Promise((resolve, reject) => {
                const idPath = this.cacheAreaMap[data.pId] || data.pId;
                let areaListData = this.$utils.Comm.pluckDeep(idPath, this.cacheArea, true).children;
                if (areaListData) {
                    // this.setAreaList(areaListData);
                    return resolve(areaListData);
                }

                this.cancelSource = this.$utils.Comm.createCancelToken();
                this.$axios
                    .get('/rpc/comm/rs/region/getChild', {
                        ...data,
                        _cancelSource: this.cancelSource.token,
                    })
                    .then(json => {
                        const res = json.data.data;
                        const areaListData = this.setCacheArea(res, idPath);
                        // this.setAreaList(areaListData);
                        return resolve(areaListData);
                    })
                    .catch(reject);
            });
        },
        getLoadSelectNode(data) {
            this.cancelSource && this.cancelSource.cancel({ code: 1, msg: '取消子查询' });
            return this.$axios.get('/rpc/comm/rs/region/loadAllArea', data).then(json => {
                const res = json.data.data;
                let idPath = this.pId + '';
                const resolveArr = [];
                res.forEach(area => {
                    resolveArr.push(this.setCacheArea(area, idPath));
                    const selectedArea = (area => area.find(item => item.isSelect))(area);
                    idPath = `${idPath}.${selectedArea.id}`;
                });
                return Promise.resolve(resolveArr);
            });
        },
        setCacheArea(data, idPath = this.pId) {
            if (!Array.isArray(data)) return;
            idPath = idPath + '';
            let res = [];
            data.forEach(area => {
                const idPathArr = idPath.split('.');
                const pId = idPathArr[idPathArr.length - 1];

                const currIdPath = `${idPath}.${area.id}`;
                this.cacheAreaMap[area.id] = currIdPath;

                res.push({
                    ...area,
                    pId,
                    idPath: currIdPath,
                });
            });

            const cacheArea = this.$utils.Comm.pluckDeep(idPath, this.cacheArea, true);
            cacheArea.children = res;
            return res;
        },
        setAreaList(data) {
            if (data.length) {
                this.areaList = data;
            } else {
                this.doClose();
            }
        },
    },
};
